package com.hw.common;

/**
 * http响应状态码
 *
 * @author whw
 * @date 2021/6/11
 */
public enum ResponseCode {
    /**
     * 操作成功
     */
    SUCCESS(200, "操作成功"),

    /**
     * 未授权
     */
    UNAUTHORIZED(401, "未授权"),

    /**
     * 缺少token
     */
    MISSING_TOKEN(401, "缺少token"),

    /**
     * 权限不足
     */
    NOT_PERMISSION(403, "权限不足"),

    /**
     * 请求的资源不存在
     */
    NOT_FOUND(404, "请求的资源不存在"),

    /**
     * 操作失败
     */
    FAIL(500, "操作失败");

    private final int code;
    private final String msg;

    ResponseCode(final int code, final String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    /**
     * 根据code获取
     *
     * @param code
     * @return
     */
    public static ResponseCode getByCode(int code) {
        ResponseCode[] ecs = ResponseCode.values();
        for (ResponseCode ec : ecs) {
            if (ec.getCode() == code) {
                return ec;
            }
        }
        return null;
    }
}
